package com.myapplication.ui.myactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapplication.R;
import com.myapplication.ui.homefragment.shopfragment.ShopFragmentAdapter;
import com.myapplication.utils.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by zitaojiang on 2016/10/19.
 */
public class MainInfoActivity extends AppCompatActivity {

    @Bind(R.id.lv)
    ListView lv;
    private ShopFragmentAdapter adapter;
    private String objectId;
    private Collection_Adapter mAdapter;
    private ListView listView;
    List<MyCollection> mList;

    @Bind(R.id.iv_icon)
    ImageView ivIcon;
    @Bind(R.id.tv_name)
    TextView tvName;
    private String name;
    private BmobFile icon;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maininfo);
        ButterKnife.bind(this);
//         initView();
        initListView();

    }

    private void initListView() {
        BmobUser user = BmobUser.getCurrentUser();
        String mUser = user.getUsername();
        tvName.setText(mUser);
        BmobQuery<MyCollection> query = new BmobQuery<>();
        query.addWhereEqualTo("myName", mUser);
        query.setLimit(50);
        query.findObjects(new FindListener<MyCollection>() {
            @Override
            public void done(List<MyCollection> list, BmobException e) {
                /*mList=list;
                mAdapter=new Collection_Adapter(mList,MainInfoActivity.this);
                listView.setAdapter(mAdapter);*/
                if (e == null) {
                    mList = list;
                    mAdapter = new Collection_Adapter(mList, MainInfoActivity.this);
                    lv.setAdapter(mAdapter);

                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());

                }

            }
        });

    }

    private void initView() {
        Intent intent = getIntent();
        objectId = intent.getStringExtra("objectid");
        BmobQuery<MyUser> query = new BmobQuery<>();
        if (!objectId.isEmpty()) {
            query.getObject(objectId, new QueryListener<MyUser>() {
                @Override
                public void done(MyUser myUser, BmobException e) {
                    if (e == null) {
                        name = myUser.getName();
                        //icon = myUser.getIcon();
                        String url = icon.getFileUrl();
                        //Glide.with(MainInfoActivity.this).load(url).into(ivIcon);
                        Picasso.with(MainInfoActivity.this).load(url).transform(new CircleTransform()).into(ivIcon);
                        Toast.makeText(MainInfoActivity.this, "加载成功" + url, Toast.LENGTH_SHORT).show();
                        Log.i("tag", "done: " + url);
                        adapter = new ShopFragmentAdapter(objectId, name);
                    }
                }
            });
        } else {
            Toast.makeText(MainInfoActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
        }

    }
}
